package com.curvebreakers.wheresmystuff.activities;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.DBResultListener;
import com.curvebreakers.wheresmystuff.model.DatabaseModel;
import com.curvebreakers.wheresmystuff.model.Entry;
import com.curvebreakers.wheresmystuff.model.EntryType;
import com.curvebreakers.wheresmystuff.model.Session;

/**
 * A fragment representing a single Entry detail screen. This fragment is either
 * contained in a {@link EntryListActivity} in two-pane mode (on tablets) or a
 * {@link EntryDetailActivity} on handsets.
 */
public class EntryDetailFragment extends Fragment implements DBResultListener {
	Button entryStatusChange;
	/**
	 * The dummy content this fragment is presenting.
	 */
	private Entry entry;
	private ProgressDialog pd;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public EntryDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey("entry")){ 
			entry = getArguments().getParcelable("entry");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.fragment_entry_detail,
				container, false);

		if (entry != null) {
			((TextView) rootView.findViewById(R.id.entry_detail_title)).setText(entry.getTitle());
			((TextView) rootView.findViewById(R.id.entry_detail_zip_code)).setText(entry.getZipCode());
			((TextView) rootView.findViewById(R.id.entry_detail_description)).setText(entry.getDescription());
			((TextView) rootView.findViewById(R.id.entry_detail_category)).setText(entry.getCategory().toString());
			((TextView) rootView.findViewById(R.id.entry_detail_status)).setText(entry.getStatusString());
			((TextView) rootView.findViewById(R.id.entry_detail_email)).setText(entry.getUserEmail());
			((TextView) rootView.findViewById(R.id.entry_detail_date)).setText(entry.getDateTimeString());
			((TextView) rootView.findViewById(R.id.entry_detail_reward)).setText("$" + entry.getReward());
			entryStatusChange = ((Button) rootView.findViewById(R.id.entry_status_change));
			
			if (entry.getUserId() == Session.getSession().getUser().getId()) {
				entryStatusChange.setVisibility(View.VISIBLE);
				if (entry.isResolved()) {
					entryStatusChange.setText("Mark Unresolved");
					
				} else {
					entryStatusChange.setText("Mark Resolved");
				}
				entryStatusChange.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						entry.setResolved(!entry.isResolved());
						((TextView) rootView.findViewById(R.id.entry_detail_status)).setText(entry.getStatusString());
						if (entry.isResolved()) {
							entryStatusChange.setText("Mark Unresolved");
							
						} else {
							entryStatusChange.setText("Mark Resolved");
						}
						DatabaseModel.saveEntry(entry, EntryDetailFragment.this);
						pd = new ProgressDialog(EntryDetailFragment.this.getActivity(), ProgressDialog.THEME_TRADITIONAL);
						pd.setTitle("Saving entry");
						pd.show();
					}
				});
			}
			
			System.out.println("email : "+entry.getUserEmail());
			if (entry.getEntryType() != EntryType.LOST)
				rootView.findViewById(R.id.entry_detail_reward_container).setVisibility(View.GONE);
		}

		return rootView;
	}

	@Override
	public void onDBResultReceived(Object obj) {
		Toast.makeText(this.getActivity(), "Entry saved successfully", Toast.LENGTH_SHORT).show();
		pd.dismiss();
	}

	@Override
	public void onDBRequestFailure(String message) {
		Toast.makeText(this.getActivity(), "Entry not saved", Toast.LENGTH_SHORT).show();
		pd.dismiss();
	}
}
